/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package olympicgameslogic;


import java.io.*;
import javax.swing.JOptionPane;

/**
 *
 * @author 75
 */
public class DataExporter implements Serializable{

   public void exportHtml(LinkedList list, int type) throws IOException {
      switch (type) {
	 case 1:
	    exportHtmlNations(list);
	    break;

	 case 2:
	    exportHtmlSports(list);
	    break;
	 case 3:
	 //exportEditions(list);
	    break;
	 case 4:
	    //exportEditionsResults(list);
	    break;
	 case 5:
	    //exportAthletes(list);
	    break;
      }







   }

   public void exportHtmlNations(LinkedList nationsList) throws IOException {
      String saveas = JOptionPane.showInputDialog(null, "Introduza o nome do ficheiro de destino (sem extensao)", "Novo ficheiro...", 1);
      if (saveas != null && !saveas.equals("")) {
	 saveas = saveas + ".html";
	 File fdest = new File(saveas);
	 while (fdest.exists()) {
	    saveas = JOptionPane.showInputDialog(null, saveas + " ja existe!\nIntroduza o nome do ficheiro de destino (sem extensao)", "Erro", JOptionPane.ERROR_MESSAGE);
	    saveas = saveas + ".html";
	    fdest = new File(saveas);
	 }


	 String titulo = "Lista de nações no sistema. Total: " + nationsList.getSize();

	 Writer output = null;
	 output = new BufferedWriter(new FileWriter(fdest));

	 output.write("<html>\n");
	 output.write("<head>\n");
	 output.write("</head>\n");

	 output.write("<body>\n");

	 output.write("<p><b><font face=\"Tahoma\" size=\"5\">" + titulo + "</font></b></p>\n");


	 output.write("<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#111111\" width=\"46%\" id=\"AutoNumber1\" height=\"20\">\n");
	 output.write("<tr>\n");
	 output.write("<td width=\"53%\" height=\"20\" bordercolor=\"#666666\" bgcolor=\"#666666\"><b>\n");
	 output.write("<font face=\"Verdana\" size=\"2\" color=\"#FFFFFF\">&nbsp;País</font></b></td>\n");
	 output.write("<td width=\"17%\" height=\"20\" bordercolor=\"#666666\" bgcolor=\"#666666\"><b>\n");
	 output.write("<font face=\"Verdana\" size=\"2\" color=\"#FFFFFF\">&nbsp;Código</font></b></td>\n");

	 output.write("<td width=\"12%\" height=\"20\" bordercolor=\"#000000\" bgcolor=\"#666666\"><b>\n");
	 output.write("<font face=\"Verdana\" size=\"2\" color=\"#FFFFFF\">&nbsp;Outros Códigos</font></b></td>\n");
	 output.write("</tr>\n");
	 for (int i = 1; i <= nationsList.getSize(); i++) {
	    output.write("<tr>\n");
	    output.write("<td width=\"53%\" height=\"20\"><font face=\"Verdana\" size=\"2\">&nbsp;" + ((Nation) nationsList.getElement(i)).getName() + "</font></td>\n");
	    output.write("<td width=\"17%\" height=\"20\"><font face=\"Verdana\" size=\"2\">&nbsp;" + ((Nation) nationsList.getElement(i)).getCode() + "</font></td>\n");
	    output.write("<td width=\"17%\" height=\"20\"><font face=\"Verdana\" size=\"2\">&nbsp;" + ((Nation) nationsList.getElement(i)).getCode() + "</font></td>\n");
	    output.write("</tr>\n");
	 }

	 output.write("</table>\n");

	 output.write("</body>\n");

	 output.write("</html>\n");
	 output.close();
	 JOptionPane.showMessageDialog(null, "Ficheiro html criado com sucesso!", "Ficheiro criado", 1);
      }

   }

   public void exportHtmlSports(LinkedList sportsList) throws IOException {

      String saveas = JOptionPane.showInputDialog(null, "input the name of the file. Without extension", "New file...", 1);
      if (saveas != null && !saveas.equals("")) {
	 saveas = saveas + ".html";
	 File fdest = new File(saveas);
	 while (fdest.exists()) {
	    saveas = JOptionPane.showInputDialog(null, saveas + " ja existe!\nIntroduza o nome do ficheiro de destino (sem extensao)", "Erro", JOptionPane.ERROR_MESSAGE);
	    saveas = saveas + ".html";
	    fdest = new File(saveas);
	 }
	 String titulo = "Lista de Modalidades, disciplinas e provas";

	 Writer output = null;
	 output = new BufferedWriter(new FileWriter(fdest));

	 output.write("<html>\n");
	 output.write("<head>\n");
	 output.write("</head>\n");

	 output.write("<body>\n");

	 output.write("<p><b><font face=\"Tahoma\" size=\"5\">" + titulo + "</font></b></p>\n");


	 output.write("<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#111111\" width=\"46%\" id=\"AutoNumber1\" height=\"20\">\n");
	 output.write("<tr>\n");
	 output.write("<td width=\"53%\" height=\"20\" bordercolor=\"#666666\" bgcolor=\"#666666\"><b>\n");
	 output.write("<font face=\"Verdana\" size=\"2\" color=\"#FFFFFF\">&nbsp;Modalidade</font></b></td>\n");
	 output.write("<td width=\"17%\" height=\"20\" bordercolor=\"#666666\" bgcolor=\"#666666\"><b>\n");
	 output.write("<font face=\"Verdana\" size=\"2\" color=\"#FFFFFF\">&nbsp;Disciplina</font></b></td>\n");
	 output.write("<td width=\"12%\" height=\"20\" bordercolor=\"#000000\" bgcolor=\"#666666\"><b>\n");
	 output.write("<font face=\"Verdana\" size=\"2\" color=\"#FFFFFF\">&nbsp;Tipo</font></b></td>\n");
	 output.write("<td width=\"12%\" height=\"20\" bordercolor=\"#000000\" bgcolor=\"#666666\"><b>\n");
	 output.write("<font face=\"Verdana\" size=\"2\" color=\"#FFFFFF\">&nbsp;Sexo</font></b></td>\n");
	 output.write("<td width=\"12%\" height=\"20\" bordercolor=\"#000000\" bgcolor=\"#666666\"><b>\n");
	 output.write("<font face=\"Verdana\" size=\"2\" color=\"#FFFFFF\">&nbsp;Unidade de Medida</font></b></td>\n");
	 output.write("<td width=\"12%\" height=\"20\" bordercolor=\"#000000\" bgcolor=\"#666666\"><b>\n");
	 output.write("<font face=\"Verdana\" size=\"2\" color=\"#FFFFFF\">&nbsp;Ordenação</font></b></td>\n");
	 output.write("</tr>\n");

	 for (int i = 1; i <= sportsList.getSize(); i++) {
	    Modality m = (Modality) sportsList.getElement(i);

	    for (int j = 0; j < m.getDisciplines().getSize(); j++) {
	       Discipline d = (Discipline) m.getDisciplines().getElement(j);
	       for (int k = 0; k < d.getEventsList().getSize(); k++) {
		  DisciplineEvent de = (DisciplineEvent) d.getEventsList().getElement(k);
		  output.write("<tr>\n");
		  output.write("<td width=\"53%\" height=\"20\"><font face=\"Verdana\" size=\"2\">&nbsp;" + m.getName() + "</font></td>\n");
		  output.write("<td width=\"17%\" height=\"20\"><font face=\"Verdana\" size=\"2\">&nbsp;" + d.getName() + "</font></td>\n");
		  output.write("<td width=\"17%\" height=\"20\"><font face=\"Verdana\" size=\"2\">&nbsp;" + d.getType().toString() + "</font></td>\n");
		  output.write("<td width=\"17%\" height=\"20\"><font face=\"Verdana\" size=\"2\">&nbsp;" + de.getEventGender() + "</font></td>\n");
		  output.write("<td width=\"17%\" height=\"20\"><font face=\"Verdana\" size=\"2\">&nbsp;" + d.getcType().getUnit() + "</font></td>\n");
		  output.write("<td width=\"17%\" height=\"20\"><font face=\"Verdana\" size=\"2\">&nbsp;" + d.getcType().getOrder() + "</font></td>\n");
		  output.write("</tr>\n");



	       }

	    }

	 }

	 output.write("</table>\n");

	 output.write("</body>\n");

	 output.write("</html>\n");
	 output.close();
	 JOptionPane.showMessageDialog(null, "Ficheiro html criado com sucesso!", "Ficheiro criado", 1);
      }
   }
   
   public void exportHtmlResults(LinkedList resultsList) throws IOException{
      
   }
   
   public void exportUsers(LinkedList<AppUser> usersList){
       try {     
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("userdata.bin"));
            out.writeObject(usersList);
            out.close();
            } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Fechar Aplicação", JOptionPane.ERROR_MESSAGE);}
   }
   
}
